package com.szxydb.dmp.admin.system.service.impl;

import com.szxydb.dmp.admin.system.model.TokenModel;
import com.szxydb.dmp.admin.system.service.TokenManager;
import com.szxydb.dmp.common.CommonResult;
import com.szxydb.dmp.constants.Constants;
import com.szxydb.dmp.exception.CrmebException;
import com.szxydb.dmp.utils.RedisUtil;
import com.szxydb.dmp.utils.ThreadLocalUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class TokenManagerImpl implements TokenManager {

    @Resource
    private HttpServletRequest request;

    @Resource
    protected RedisUtil redisUtil;

    /**
     * 生成token
     * @param account
     * @param value
     * @param modelName
     * @return
     */
    @Override
    public TokenModel createToken(String account, String value, String modelName) {
        String _token  = UUID.randomUUID().toString().replace("-","");
        TokenModel token = new TokenModel(account,_token);
        token.setUserNo(account);
        String clientType = request.getParameter("clienttype");
        token.setClienttype(clientType == null ? "Web" :clientType);
        token.setHost(request.getRemoteHost());
        token.setLastAccessedTime(System.currentTimeMillis());

        redisUtil.set(modelName+_token,value, Constants.TOKEN_EXPRESS_MINUTES, TimeUnit.MINUTES);

        Map<String,Object> hashedMap = new HashMap<>();
        hashedMap.put(modelName,value);
        ThreadLocalUtil.set(hashedMap);
        return token;
    }

    /**
     * 检测token
     * @param token
     * @param modelName
     * @return
     */
    @Override
    public boolean checkToken(String token, String modelName) {
        return redisUtil.exists(modelName+token);
    }

    @Override
    public TokenModel getToken(String token, String modelName) {
        Object o = redisUtil.get(modelName + token);
        TokenModel tokenModel = new TokenModel();
        tokenModel.setUserId(Integer.parseInt(o.toString()));
        return tokenModel;
    }

    @Override
    public String getCurrentClienttype(String userno) {
        return null;
    }

    @Override
    public void deleteToken(String token, String modelName) {
        redisUtil.remove(modelName+token);
    }

    @Override
    public Integer getUserCount() {
        return null;
    }

    @Override
    public CommonResult<TokenModel> getOnlineUsers(Integer pageNo, Integer pageSize) {
        return null;
    }

    @Override
    public TokenModel getRealToken(String userno) {
        return null;
    }

    @Override
    public String getLocalInfoException(String key) {
        Object value = ThreadLocalUtil.get(key);
        if (value == null){
            throw new CrmebException("登录信息已过期，请重新登录！");
        }
        return value.toString();
    }

    @Override
    public Object getLocalInfo(String key) {
        if (StringUtils.isNotBlank(key)){
            return ThreadLocalUtil.get(key);
        }
        return null;
    }

    @Override
    public Integer getLocalUserId() {
        return Integer.parseInt(getLocalInfoException("id"));
    }
}
